package com.da.goal.service;

import com.da.common.enums.ResultEnum;
import com.da.common.exception.BusinessException;
import com.da.common.exception.ParameterException;
import com.da.common.utils.SnowflakeIdWorker;
import com.da.common.utils.TimestampUtil;
import com.da.goal.entity.DailyGoalCategories;
import com.da.goal.entity.DailyGoalTags;
import com.da.goal.entity.constant.GoalsConstant;
import com.da.goal.entity.vo.DailyCategoriesTagVO;
import com.da.goal.mapper.DailyGoalCategoriesMapper;
import com.da.goal.mapper.DailyGoalTagsMapper;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.swing.text.html.Option;
import java.util.*;

/**
 * @Author: hyacinth
 * @CreateTime: 2025-02-13
 * @Version: 1.0
 */
@Service("dailyGoalsCategoriesService")
@RequiredArgsConstructor
@Slf4j
public class DailyGoalsCategoriesService {

    private final DailyGoalCategoriesMapper dailyGoalsCategoriesMapper;

    private final DailyGoalTagsMapper dailyGoalTagsMapper;

    /**
     * 创建目标分类
     * @param dailyGoalCategories 目标分类实体类
     * @return
     */
    public boolean createGoalCategories(DailyGoalCategories dailyGoalCategories) {
        try{
            dailyGoalCategories.setId(String.valueOf(SnowflakeIdWorker.generateId()));
            dailyGoalCategories.setCreatedAt(TimestampUtil.now());
            dailyGoalCategories.setIsDeleted(GoalsConstant.IS_NOT_DELETE);
            int isCreateDailyGoalCategories = dailyGoalsCategoriesMapper.insert(dailyGoalCategories);
            return isCreateDailyGoalCategories > 0;
        }catch (Exception e){
            log.error("创建目标分类失败，原因：{}", e.getMessage());
            return false;
        }
    }

    /**
     * 树状查询目标分类信息
     */
    public List<DailyCategoriesTagVO> queryTreeGoalCategoriesInfo() {
        // 查询所有的分类数据
        List<DailyGoalCategories> dailyGoalCategories = dailyGoalsCategoriesMapper.queryAll();
        // 查询所有的标签数据
        List<DailyGoalTags> dailyGoalTags = dailyGoalTagsMapper.queryAll();
        // 创建一个空的列表，用于存储树状结构的数据
        List<DailyCategoriesTagVO> dailyCategoriesTagVOList = new ArrayList<>();

        // 遍历所有的分类数据，构建树状结构
        for (DailyGoalCategories categories : dailyGoalCategories) {
            DailyCategoriesTagVO dailyCategoriesTagVO = new DailyCategoriesTagVO();
            dailyCategoriesTagVO.setId(categories.getId());
            dailyCategoriesTagVO.setCategoriesName(categories.getCategoriesName());
            dailyCategoriesTagVO.setCreatedAt(categories.getCreatedAt());
        }

        // 遍历所有的标签数据，将其添加到对应的分类下
        for (DailyGoalTags tag : dailyGoalTags) {
            for (DailyCategoriesTagVO dailyCategoriesTagVO : dailyCategoriesTagVOList) {
                if (tag.getCategoriesId().equals(dailyCategoriesTagVO.getId())) {
                    dailyCategoriesTagVO.getChildren().add(tag);
                }
            }
        }
        // 返回树状结构的数据
        return dailyCategoriesTagVOList;
    }





}
